package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.ListConfigPO;
import com.falcon.domain.dos.ListConfigDO;
import com.falcon.domain.req.ListConfigQuery;
import com.falcon.domain.req.ListConfigRequest;

import com.falcon.mapper.IListConfigMapper;
import com.falcon.service.IListConfigService;


@Slf4j
@Service
public class ListConfigService implements IListConfigService {

    @Autowired
    private IListConfigMapper listConfigMapper;

    @Override
    public Long save(ListConfigRequest request){
        ListConfigPO listConfigPO = BeanUtil.copyProperties(request, ListConfigPO.class);

        this.listConfigMapper.insertNotNull(listConfigPO);

        return listConfigPO.getId();
    }

    @Override
    public Boolean update(Long id, ListConfigRequest request){

        ListConfigPO listConfigPO = BeanUtil.copyProperties(request, ListConfigPO.class);
        listConfigPO.setId(id);

        return this.listConfigMapper.updateNotNull(listConfigPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.listConfigMapper.deleteById(id);
    }

    @Override
    public ListConfigDO queryById(Long id){

        ListConfigPO listConfigPO = this.listConfigMapper.selectById(id);

        ListConfigDO listConfigDO = BeanUtil.copyProperties(listConfigPO, ListConfigDO.class);

        return listConfigDO;
    }


    @Override
    public List<ListConfigDO> queryList(ListConfigQuery request){

        Condition condition = request.toCondition();

        List<ListConfigPO> poList = this.listConfigMapper.selectList(condition);

        List<ListConfigDO> doList = BeanUtil.copyToList(poList, ListConfigDO.class);

        return doList;
    }

    @Override
    public BasePager<ListConfigDO> queryPager(ListConfigQuery request){


        PageInfo<ListConfigPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.listConfigMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, ListConfigDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.listConfigMapper.deleteByIds(ids);
    }


}
